package com.jerome.fs.util;

import com.jerome.fs.json.JsonParseException;
import com.jerome.fs.json.JsonParser;
import com.jerome.fs.json.JsonSerializer;

import java.lang.reflect.InvocationTargetException;

/**
 * JSON utility class.
 * @author Jerome
 * @date 2023/7/31
 */
public class JsonUtil {
    /**
     * Serialize an object to a JSON string.
     * @param object The object to be serialized.
     * @return The JSON string representing the serialized object.
     * @throws InvocationTargetException if an error occurs during serialization.
     * @throws IllegalAccessException if there's an illegal access to the object.
     */
    public static String writeValueAsString(Object object) throws InvocationTargetException, IllegalAccessException {
        return JsonSerializer.serialize(object);
    }

    /**
     * Deserialize a JSON string to an object.
     * @param jsonString The JSON source string.
     * @param <T> The type of the object to deserialize.
     * @return The deserialized object.
     * @throws IllegalAccessException if there's an illegal access to the object.
     * @throws JsonParseException if an error occurs during deserialization.
     * @throws InstantiationException if there's an issue with object instantiation.
     */
    public static <T> T readValue(String jsonString, Class<T> type) throws IllegalAccessException, JsonParseException, InstantiationException {
        return JsonParser.parse(jsonString, type);
    }
}
